/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Aptech.DAL;

import Aptech.DALInterface.DocumentInterface;
import HibernateMappingPoJo.CatalogyDocument;
import HibernateMappingPoJo.Documnets;
import HibernateMappingPoJo.Lands;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 * 
 * @author nkhoan
 */
public class MgDocument implements DocumentInterface {

	private SessionFactory sessFact;
	private Session sess;
	private Transaction tr;

	@Override
	public List getAllDocument() {
		List list = null;
		try {
			sessFact = Hibernate.HibernateUtil.getSessionFactory();
			sess = sessFact.openSession();
			list = sess.createCriteria(Documnets.class).list();
			sess.flush();
		} catch (HibernateException he) {
			System.out.println(he.getMessage());
		} finally {
			sess.close();

		}

		return list;
	}

	@Override
	public Documnets getDocByID(String id) {
		Documnets doc = null;
		try {
			sessFact = Hibernate.HibernateUtil.getSessionFactory();
			sess = sessFact.openSession();
			Criteria criteria = sess.createCriteria(Documnets.class).add(
					Restrictions.eq("id", id));
			doc = (Documnets) criteria.uniqueResult();
			sess.flush();
		} catch (HibernateException he) {
			System.out.println(he.getMessage());
		} finally {
			sess.close();
		}

		return doc;
	}

	@Override
	public void deleteDoc(String id) {
		try {
			sessFact = Hibernate.HibernateUtil.getSessionFactory();
			sess = sessFact.openSession();
			String sql = "delete Documnets where ID = '" + id + "'";
			sess.createSQLQuery(sql).executeUpdate();
			sess.flush();
		} catch (HibernateException he) {
			System.out.println(he.getMessage());
		} finally {
			sess.close();

		}
	}

	@Override
	public void editDocuments(Documnets doc) {

		try {
			sessFact = Hibernate.HibernateUtil.getSessionFactory();
			sess = sessFact.openSession();
			tr = sess.beginTransaction();

			sess.update(doc);
			tr.commit();
			sess.flush();
		} catch (HibernateException he) {
			System.out.println(he.getMessage());
		} finally {
			sess.close();
		}
	}

	@Override
	public Documnets getDocByName(String name) {

		return null;
	}

	@Override
	public void addDoc(Documnets doc) {
		try {
			sessFact = Hibernate.HibernateUtil.getSessionFactory();
			sess = sessFact.openSession();
			tr = sess.beginTransaction();
			sess.save(doc);
			tr.commit();
			sess.flush();
		} catch (HibernateException he) {
			System.out.println(he.getMessage());
		} finally {
			sess.close();
		}
	}

	@Override
	public void saveFile(String fileName, String filePath, byte[] fileData)
			throws FileNotFoundException, IOException {
		File fileToCreate = new File(filePath, fileName);
		// If file does not exists create file
		if (!fileToCreate.exists()) {
			FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
			fileOutStream.write(fileData);
			fileOutStream.flush();
			fileOutStream.close();
		}
	}

	public boolean deleteFile(String filePath, String fileName) {
		File file = new File(filePath, fileName);
		if (file.exists()) {
			return file.delete();
		}
		return false;
	}

	@Override
	public List getAllCatelogyDoc() {
		List list = null;
		try {
			sessFact = Hibernate.HibernateUtil.getSessionFactory();
			sess = sessFact.openSession();
			list = sess.createCriteria(CatalogyDocument.class).list();
			sess.flush();
		} catch (HibernateException he) {
			System.out.println(he.getMessage());
		} finally {
			sess.close();
		}
		return list;
	}

	@Override
	public void downloadFile(String fAddress, String localFileName,
			String destinationDir) {
		int size = 1024;
		OutputStream outStream = null;
		URLConnection uCon = null;

		InputStream is = null;
		try {
			URL Url;
			byte[] buf;
			int ByteRead, ByteWritten = 0;
			Url = new URL(fAddress);
			outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "\\" + localFileName));

			uCon = Url.openConnection();
			is = uCon.getInputStream();
			buf = new byte[size];
			while ((ByteRead = is.read(buf)) != -1) {
				outStream.write(buf, 0, ByteRead);
				ByteWritten += ByteRead;
			}
			System.out.println("Downloaded Successfully.");
			System.out.println("File name:\"" + localFileName+ "\"\nNo ofbytes :" + ByteWritten);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
				outStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
